package flexlingo
{
	
	//-locale=ar,zh,zh_CN,zh-TW,nl,en,en_US,fr,de,el,it,ja,ko,pt,ru,es 
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import flexlingo.requests.LingoRequest;
	import flexlingo.responses.LingoResponse;
	import flexlingo.vo.LangPackVO;
	import flexlingo.vo.LangPairVO;
	import flexlingo.vo.LanguageVO;
	
	import mx.collections.ArrayCollection;
	import mx.managers.ISystemManager;
	import mx.resources.ResourceManager;
	import mx.rpc.AsyncToken;
	
	
	[Event(name="lingoStringChanged",type="flash.events.Event")]
	
	[Mixin]
	[Bindable]
	public class Lingo extends EventDispatcher
	{
		
		private static var systemManager : ISystemManager;
		
		public static function init(systemManager:ISystemManager):void
		{	
			
			Lingo.systemManager = systemManager;
			//var resourceBundle : ResourceBundle = new ResourceBundle(null,"Lingo");
				
			//ResourceManager.getInstance().addResourceBundle( resourceBundle );
	
		}
		
		
		//public static const ROOT_URL : String = "http://www.google.com/uds/Gtranslate";
		public static const ROOT_URL : String = "http://ajax.googleapis.com/ajax/services/language/translate";
		
		
		public static const bundleName : String = "Lingo";
		
	
		public static var defaultLangCode : String = "en";
	
		public static var currentLangCode : String = "en";
		
		
		[Bindable(event="lingoStringChanged")]
		public static function phrase(string : String):LangPackVO
		{
			var key : LangPackVO = Lingo.langpack[string];
			
			if( key == null)
			{	
				key =  new LangPackVO(string,string);
				Lingo.langpack[string] = key;
			} 
	
			return key;
		}
		
	
	
		public static var langpack : Object = {};
	
	
		[Bindable(event="lingoLangPackCollectionChanged")]
		public static function getLangpackCollection():ArrayCollection
		{

			var array : Array = [];
			
			for each( var item : LangPackVO in Lingo.langpack )
			{	
				array.push(item);
			}
			
			return new ArrayCollection(array);
		}
		
		
		
		
		public static function translate(text:String,langpair:LangPairVO,result:Function,fault:Function=null):AsyncToken
		{		
			var request : LingoRequest = new LingoRequest(text,langpair,result,fault);
			return request.send();
		}
		
		
		
	/* 	public function changeLanguage(langCode:String=null):void
		{
			if(langCode == null)
		  	{
		  		ResourceManager.getInstance().localeChain = [ Lingo.defaultLangCode ];
		  	}
		  	else
		  	{
		  		ResourceManager.getInstance().localeChain = [ langCode ];
		  	} 
		} */
		
		
		public static function translateLangpack(langCode:String):void
		{
			

			var fromLang : LanguageVO = Language.getLangByCode( Lingo.defaultLangCode );  
	
			var toLang : LanguageVO = Language.getLangByCode( langCode );
			
			
			var langpair : LangPairVO = new LangPairVO(fromLang,toLang);
			
			for each( var item : LangPackVO in Lingo.langpack)
			{
				Lingo.translate( item.key, langpair, Lingo.translateLangpackResult );
			}
			
			Lingo.currentLangCode = langCode;

		}
		
		private static function translateLangpackResult(response:LingoResponse):void
		{
			Lingo.setString(response.originalText,response.translatedText);
		}
		
		public static function setString(string:String,value:String=null):void
		{
			
			if(value == null)
				return void;
			
			var key : LangPackVO = Lingo.langpack[string];
			
			if( key != null)
			{	
				key.value = value;		
			} 
			
			//key.dispatchEvent(new Event
			
		}
		
		
		
		
	

	}
}